package com.dream.technology.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dream.technology.db.entity.RcFieldSelect;
import com.dream.technology.db.entity.RcFieldSelectQuery;
import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.service.RcFieldSelectService;
import com.dream.technology.vo.ErrorCode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/risk/admin/field_select")
public class RcFieldSelectController {

    private static final Logger logger = LoggerFactory.getLogger(RcFieldSelectController.class);

    @Autowired
    RcFieldSelectService rcFieldSelectService;

    /**
     * 字段值
     * @param pageNum
     * @param pageSize
     * @param fieldName
     * @param typeId
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize, String fieldName, Integer typeId){

        RcFieldSelectQuery rcFieldSelectQuery=new RcFieldSelectQuery();
        PageHelper.startPage(pageNum,pageSize);
        List<RcFieldSelect> RcFieldSelectList = rcFieldSelectService.selectByExample(rcFieldSelectQuery);
        PageInfo pageInfo = new PageInfo(RcFieldSelectList);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), pageInfo);
    }

    /**
     *   添加字段值
     * @param RcFieldSelect
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<Object> addFieldSelect(RcFieldSelect RcFieldSelect){

        int insert = rcFieldSelectService.insert(RcFieldSelect);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);

    }

    /**
     * 获取字段值回显接口
     * @param id
     * @return
     */
    @GetMapping("/id")
    public GenericRspDTO<RcFieldSelect> getFieldSelectById(Long id){

        RcFieldSelect RcFieldSelect = rcFieldSelectService.selectByPrimaryKey(id);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), RcFieldSelect);
    }



    /**
     * 根据字段id获取字段值回显接口
     * @param id
     * @return
     */
    @GetMapping("/selectByFieldId")
    public GenericRspDTO<  List<RcFieldSelect>> selectByFieldId(Long id){
        RcFieldSelectQuery query = new RcFieldSelectQuery();
        RcFieldSelectQuery.Criteria criteria = query.createCriteria();
        criteria.andFieldIdEqualTo(id);
        List<RcFieldSelect> rcFieldSelects = rcFieldSelectService.selectByExample(query);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcFieldSelects);
    }

    /**
     * 修改字段值接口 (逻辑删除)
     * @param RcFieldSelect
     * @return
     */
    @PostMapping("/update")
    public GenericRspDTO<Object> updateFieldSelect(RcFieldSelect RcFieldSelect){

        rcFieldSelectService.updateByPrimaryKeySelective(RcFieldSelect);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     *  删除字段值接口 (逻辑删除)
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public GenericRspDTO<Object> deleteFieldSelect(Long id){

        rcFieldSelectService.deleteByPrimaryKey(id);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }
}
